#include <stdio.h>
#include "nb_Server.h"

class Echo
{
public:
	static void OnConnected(NB_Request * lpRequest, NB_Response * lpResponse)
	{
		printf("+ connected from [%s:%d]\n", lpRequest->getIp(), lpRequest->getPort());
		printf("-\n\n");
	}

	static void OnRecved(NB_Request * lpRequest, NB_Response * lpResponse)
	{
		printf("+ recved from [%s:%d] \n", lpRequest->getIp(), lpRequest->getPort());
		printf("data: %s \n", lpRequest->getMessage());
		printf("len: %d \n", lpRequest->getMsgLen());
		printf("-\n\n");
	}

	static void OnClosed(NB_Request * lpRequest, NB_Response * lpResponse)
	{
		printf("+ Closed from [%s:%d] \n", lpRequest->getIp(), lpRequest->getPort());
		printf("-\n\n");
	}
};


int main()
{
	int port = 888;
	int maxConnect = 10000;
	int recvThreads = 2;
	int maxHandleThreads = 100;

	printf("listen port: %d\n", port);
	printf("max connect number: %d\n", maxConnect);
	printf("recv thread number: %d\n", recvThreads);
	printf("max handle thread number: %d\n", maxHandleThreads);

	NB_Server server( port );
	server.SetMaxConnections( 10000 );
	server.SetRecvThreads(recvThreads);
	server.SetMaxHandleThreads(maxHandleThreads);
	server.SetSessionStart( Echo::OnConnected );
	server.SetSessionHandle( Echo::OnRecved );
	server.SetSessionClose( Echo::OnClosed );
	server.Run();

	printf("server run success !!!\n");
	printf("press 'q' to shutdown\n");
	for( ; getchar() != 'q' ; ) {}

	return 0;
}